import { Config } from './Class_Map.js';
import { addForecastLayer, removeForecastLayer } from './Class_Map.js';
import dayjs from 'dayjs';

// 生成基础URL
const getBaseUrl = (selectedDate, selectedTime) => {
  const dateStr = selectedDate.format('YYYYMMDD');
  return `https://www.oceanread.com/output/${dateStr}${selectedTime}`;
};

// 数据源配置
export const OceanReadWeb = {
  wind: {
    url: (selectedDate, selectedTime) => `${getBaseUrl(selectedDate, selectedTime)}/wind_G_full.png`,
    range: {
      scale: 0.25,
      startLat: 90,
      endLat: -90,
      startLon: 0,
      endLon: 360,
      width: 1440,
      height: 720,
    },
  },
  wave: {
    url: (selectedDate, selectedTime) => `${getBaseUrl(selectedDate, selectedTime)}/wave_cjk_swan.png`,
    range: {
      scale: 0.05,
      startLat: 33.70419,
      endLat: 27.666,
      startLon: 118.30423,
      endLon: 124.16101,
      width: 119,
      height: 122,
    },
  },
  current: {
    url: (selectedDate, selectedTime) => `${getBaseUrl(selectedDate, selectedTime)}/current_hl_full.png`,
    range: {
      scale: 0.25,
      startLat: 90,
      endLat: -90,
      startLon: 0,
      endLon: 360,
      width: 1440,
      height: 720,
    },
  },
  seatemp: {
    url: (selectedDate, selectedTime) => `${getBaseUrl(selectedDate, selectedTime)}/temp_ocean_full.png`,
    range: {
      scale: 0.25,
      startLat: 90,
      endLat: -90,
      startLon: 0,
      endLon: 360,
      width: 1440,
      height: 720,
    },
  },
};

// 图层状态管理
export class LayerStateManager {
  constructor() {
    this.activeLayerStates = {
      wind: {
        particle: false,
        speed: false,
        arrow: false,
        feather: false,
        label: false
      },
      current: {
        particle: false,
        speed: false,
        arrow: false,
        feather: false,
        label: false
      },
      seatemp: {
        scalar: false,
        label: false
      }
    };
  }

  // 检查数值标签依赖关系
  checkLabelDependency(type, field) {
    if (type === 'wind' && field === 'speed' && this.activeLayerStates.wind.label) {
      return false;
    }
    if (type === 'current' && field === 'speed' && this.activeLayerStates.current.label) {
      return false;
    }
    if (type === 'seatemp' && field === 'scalar' && this.activeLayerStates.seatemp.label) {
      return false;
    }
    return true;
  }

  // 更新所有图层
  updateAllLayers(selectedDate, selectedTime) {
    // 移除所有现有图层
    Object.keys(Config.activeLayers).forEach(key => {
      if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
        Config.activeLayers[key].remove();
      }
      delete Config.activeLayers[key];
    });
    removeForecastLayer();
    
    console.log('正在重新加载图层到新日期:', selectedDate.format('YYYY-MM-DD'), selectedTime + '时');
    
    // 重新加载当前激活的图层
    this.reloadActiveLayers(selectedDate, selectedTime);
    
    console.log('图层重新加载完成');
  }

  // 重新加载激活的图层
  reloadActiveLayers(selectedDate, selectedTime) {
    // 重新加载风场图层
    if (this.activeLayerStates.wind.particle) {
      this.loadWind(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.wind.speed) {
      this.loadWind_speed(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.wind.arrow) {
      this.loadWind_arrow(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.wind.feather) {
      this.loadWind_Feather(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.wind.label) {
      this.loadWind_label(selectedDate, selectedTime);
    }
    
    // 重新加载洋流图层
    if (this.activeLayerStates.current.particle) {
      this.loadCur(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.current.speed) {
      this.loadCur_speed(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.current.arrow) {
      this.loadCur_arrow(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.current.feather) {
      this.loadCur_Feather(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.current.label) {
      this.loadCur_label(selectedDate, selectedTime);
    }
    
    // 重新加载海温图层
    if (this.activeLayerStates.seatemp.scalar) {
      this.loadSeatemp(selectedDate, selectedTime);
    }
    if (this.activeLayerStates.seatemp.label) {
      this.loadSeatemp_label(selectedDate, selectedTime);
    }
  }

  // 风场相关方法
  loadWind(selectedDate, selectedTime) {
    let windData = {
      label: "10米风",
      common: {
        ...OceanReadWeb.wind,
        url: OceanReadWeb.wind.url(selectedDate, selectedTime)
      },
      attribute: { png: "wind_G_full.png", type: "wind", calRegion: false },
    };
    addForecastLayer(windData);
    this.activeLayerStates.wind.particle = true;
  }

  loadWind_speed(selectedDate, selectedTime) {
    let windData = {
      label: "10米风",
      common: {
        ...OceanReadWeb.wind,
        url: OceanReadWeb.wind.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "wind_G_full.png",
        type: "wind_speed",
        calRegion: false,
        showLand: true,
      },
    };
    addForecastLayer(windData);
    this.activeLayerStates.wind.speed = true;
  }

  loadWind_arrow(selectedDate, selectedTime) {
    let windData = {
      label: "10米风",
      common: {
        ...OceanReadWeb.wind,
        url: OceanReadWeb.wind.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "wind_G_full.png",
        type: "wind",
        calRegion: false,
        showLand: true,
        showArrow: true,
      },
    };
    addForecastLayer(windData);
    this.activeLayerStates.wind.arrow = true;
  }

  loadWind_Feather(selectedDate, selectedTime) {
    let windData = {
      label: "10米风",
      common: {
        ...OceanReadWeb.wind,
        url: OceanReadWeb.wind.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "wind_G_full.png",
        type: "wind",
        calRegion: false,
        showLand: true,
        showArrow: true,
        showFeather: true,
      },
    };
    addForecastLayer(windData);
    this.activeLayerStates.wind.feather = true;
  }

  loadWind_label(selectedDate, selectedTime) {
    let windData = {
      label: "10米风",
      common: {
        ...OceanReadWeb.wind,
        url: OceanReadWeb.wind.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "wind_G_full.png",
        type: "wind_speed",
        calRegion: false,
        showLand: true,
        showArrow: false,
        showFeather: false,
        showLabel: true,
      },
    };
    addForecastLayer(windData);
    this.activeLayerStates.wind.label = true;
  }

  removeWind() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('10米风') && (key.includes('wind') || key.includes('wind_speed'))) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
      }
    });
    Object.keys(this.activeLayerStates.wind).forEach(key => {
      this.activeLayerStates.wind[key] = false;
    });
  }

  removeWindParticle() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('10米风') && key.includes('wind') && !key.includes('showArrow') && !key.includes('showFeather') && !key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除风场粒子图层');
      }
    });
    this.activeLayerStates.wind.particle = false;
  }

  removeWindSpeed() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('10米风') && key.includes('wind_speed') && !key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除风场速度场图层');
      }
    });
    this.activeLayerStates.wind.speed = false;
  }

  removeWindArrow() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('10米风') && key.includes('wind') && key.includes('showArrow') && !key.includes('showFeather')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除风场箭头图层');
      }
    });
    this.activeLayerStates.wind.arrow = false;
  }

  removeWindFeather() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('10米风') && key.includes('wind') && key.includes('showFeather')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除风场风羽图层');
      }
    });
    this.activeLayerStates.wind.feather = false;
  }

  removeWindLabel() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('10米风') && key.includes('wind_speed') && key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除风场数值标签图层');
      }
    });
    this.activeLayerStates.wind.label = false;
  }

  // 洋流相关方法
  loadCur(selectedDate, selectedTime) {
    let Data = {
      label: "洋流",
      common: {
        ...OceanReadWeb.current,
        url: OceanReadWeb.current.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "current_hl_full.png",
        type: "current",
        calRegion: false,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.current.particle = true;
  }

  loadCur_speed(selectedDate, selectedTime) {
    let Data = {
      label: "洋流",
      common: {
        ...OceanReadWeb.current,
        url: OceanReadWeb.current.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "current_hl_full.png",
        type: "current_speed",
        calRegion: false,
        showLand: false,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.current.speed = true;
  }

  loadCur_arrow(selectedDate, selectedTime) {
    let Data = {
      label: "洋流",
      common: {
        ...OceanReadWeb.current,
        url: OceanReadWeb.current.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "current_hl_full.png",
        type: "current",
        calRegion: false,
        showLand: false,
        showArrow: true,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.current.arrow = true;
  }

  loadCur_Feather(selectedDate, selectedTime) {
    let Data = {
      label: "洋流",
      common: {
        ...OceanReadWeb.current,
        url: OceanReadWeb.current.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "current_hl_full.png",
        type: "current",
        calRegion: false,
        showLand: false,
        showArrow: true,
        showFeather: true,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.current.feather = true;
  }

  loadCur_label(selectedDate, selectedTime) {
    let Data = {
      label: "洋流",
      common: {
        ...OceanReadWeb.current,
        url: OceanReadWeb.current.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "current_hl_full.png",
        type: "current_speed",
        calRegion: false,
        showLand: false,
        showArrow: false,
        showFeather: false,
        showLabel: true,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.current.label = true;
  }

  removeCur() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('洋流') && (key.includes('current') || key.includes('current_speed'))) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
      }
    });
    Object.keys(this.activeLayerStates.current).forEach(key => {
      this.activeLayerStates.current[key] = false;
    });
  }

  removeCurParticle() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('洋流') && key.includes('current') && !key.includes('showArrow') && !key.includes('showFeather') && !key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除洋流粒子图层');
      }
    });
    this.activeLayerStates.current.particle = false;
  }

  removeCurSpeed() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('洋流') && key.includes('current_speed') && !key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除洋流速度场图层');
      }
    });
    this.activeLayerStates.current.speed = false;
  }

  removeCurArrow() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('洋流') && key.includes('current') && key.includes('showArrow') && !key.includes('showFeather')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除洋流箭头图层');
      }
    });
    this.activeLayerStates.current.arrow = false;
  }

  removeCurFeather() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('洋流') && key.includes('current') && key.includes('showFeather')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除洋流风羽图层');
      }
    });
    this.activeLayerStates.current.feather = false;
  }

  removeCurLabel() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('洋流') && key.includes('current_speed') && key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除洋流数值标签图层');
      }
    });
    this.activeLayerStates.current.label = false;
  }

  // 海温相关方法
  loadSeatemp(selectedDate, selectedTime) {
    let Data = {
      label: "海温",
      common: {
        ...OceanReadWeb.seatemp,
        url: OceanReadWeb.seatemp.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "temp_ocean_full.png",
        type: "seatemp",
        calRegion: false,
        showLand: false,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.seatemp.scalar = true;
  }

  loadSeatemp_label(selectedDate, selectedTime) {
    let Data = {
      label: "海温",
      common: {
        ...OceanReadWeb.seatemp,
        url: OceanReadWeb.seatemp.url(selectedDate, selectedTime)
      },
      attribute: {
        png: "temp_ocean_full.png",
        type: "seatemp",
        calRegion: false,
        showLand: false,
        showLabel: true,
      },
    };
    addForecastLayer(Data);
    this.activeLayerStates.seatemp.label = true;
  }

  removeSeatemp() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('海温') && key.includes('seatemp')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
      }
    });
    Object.keys(this.activeLayerStates.seatemp).forEach(key => {
      this.activeLayerStates.seatemp[key] = false;
    });
  }

  removeSeatempScalar() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('海温') && key.includes('seatemp') && !key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除海温标量场图层');
      }
    });
    this.activeLayerStates.seatemp.scalar = false;
  }

  removeSeatempLabel() {
    Object.keys(Config.activeLayers).forEach(key => {
      if (key.includes('海温') && key.includes('seatemp') && key.includes('showLabel')) {
        if (Config.activeLayers[key] && Config.activeLayers[key].remove) {
          Config.activeLayers[key].remove();
        }
        delete Config.activeLayers[key];
        console.log('已移除海温数值标签图层');
      }
    });
    this.activeLayerStates.seatemp.label = false;
  }

  // 全选功能
  loadAllWind(selectedDate, selectedTime) {
    if (!this.activeLayerStates.wind.particle) this.loadWind(selectedDate, selectedTime);
    if (!this.activeLayerStates.wind.speed) this.loadWind_speed(selectedDate, selectedTime);
    if (!this.activeLayerStates.wind.arrow) this.loadWind_arrow(selectedDate, selectedTime);
    if (!this.activeLayerStates.wind.feather) this.loadWind_Feather(selectedDate, selectedTime);
    if (!this.activeLayerStates.wind.label) this.loadWind_label(selectedDate, selectedTime);
  }

  loadAllCurrent(selectedDate, selectedTime) {
    if (!this.activeLayerStates.current.particle) this.loadCur(selectedDate, selectedTime);
    if (!this.activeLayerStates.current.speed) this.loadCur_speed(selectedDate, selectedTime);
    if (!this.activeLayerStates.current.arrow) this.loadCur_arrow(selectedDate, selectedTime);
    if (!this.activeLayerStates.current.feather) this.loadCur_Feather(selectedDate, selectedTime);
    if (!this.activeLayerStates.current.label) this.loadCur_label(selectedDate, selectedTime);
  }

  loadAllSeatemp(selectedDate, selectedTime) {
    if (!this.activeLayerStates.seatemp.scalar) this.loadSeatemp(selectedDate, selectedTime);
    if (!this.activeLayerStates.seatemp.label) this.loadSeatemp_label(selectedDate, selectedTime);
  }
}
